package com.tgy.myredis.config;

import lombok.Getter;
import lombok.Setter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisNode;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.lang.Nullable;
import org.springframework.util.StringUtils;
import redis.clients.jedis.JedisPoolConfig;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/***
 * @ClassName: RedisClusterConfig
 * @Description: 集群模式redis配置
 * @Auther: tanggy3
 * @Date: 14:13 2022/7/27
 * @version : V1.0
 */
@Configuration
@ConfigurationProperties(prefix = "rcluster")//匹配配置文件
@ConditionalOnProperty(prefix = "using", name = "redis", havingValue = "cluster")//需要有这些配置才能生效
@Setter
@Getter
public class RedisClusterConfig {
    private final Logger logger = LoggerFactory.getLogger(RedisClusterConfig.class);
    private List<String> clusterNodes;
    @Nullable
    private String password;
    private Integer maxTotal;
    private Integer maxIdle;
    private Integer minIdle;
    private Integer maxWaitMillis;

    @Bean
    public JedisConnectionFactory redisConnectionFactory() {
        JedisConnectionFactory jedisConnectionFactory = null;
        // 集群模式
        RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration();
        // 加载服务器集群节点
        try {
            // 转换成Redis点节
            Set<RedisNode> cn = new HashSet<>(clusterNodes.size());
            clusterNodes.forEach((s) -> cn.add(new RedisNode(s.split(":")[0], Integer.parseInt(s.split(":")[1]))));
            redisClusterConfiguration.setClusterNodes(cn);

            if (!StringUtils.isEmpty(password)) {
                redisClusterConfiguration.setPassword(RedisPassword.of(password));
            }

            JedisPoolConfig poolConfig = new JedisPoolConfig();
            poolConfig.setMaxIdle(maxIdle);
            poolConfig.setMaxTotal(maxTotal);
            poolConfig.setMinIdle(minIdle);
            poolConfig.setMaxWaitMillis(maxWaitMillis);

            jedisConnectionFactory = new JedisConnectionFactory(redisClusterConfiguration, poolConfig);
        } catch (Exception e) {
            logger.error("创建Redis连接工厂错误", e);
        }
        return jedisConnectionFactory;
    }

    @Bean
    public RedisTemplate<String, Object> registerRedisTemplate(JedisConnectionFactory factory) {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(factory);
        /**
         * 序列化设置
         */
        // key、hash的key 采用 String序列化方式
        redisTemplate.setKeySerializer(RedisSerializer.string());
        redisTemplate.setHashKeySerializer(RedisSerializer.string());
        // value、hash的value 采用 Jackson 序列化方式
        redisTemplate.setValueSerializer(RedisSerializer.json());
        redisTemplate.setHashValueSerializer(RedisSerializer.json());
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }

}
